syntax error, unexpected token “->”
Description
This error reports that the object syntax was not expected there.
One classic reason is the new syntax in PHP 8.4, where parenthesis are not necessary, right after a new, to call properties or methods. This syntax actually requires the parenthesis.
Another situation arise when a space is inserted between the ? and the -> operator. The space let PHP believe that a ternary operator is started, yet the -> must be applied to an object.
Example
<?php
// should be new X()->method();
new X->method();
// wrongly build null object operator
echo $obj ?
-> foo;
?>
Alternatives
- Add the parenthesis on the
newcall. - Write the
?->without spaces.